package org.jeecg.modules.lab.exam.bankQuestion.controller;

import java.util.Arrays;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.query.QueryGenerator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.jeecg.modules.lab.exam.bankQuestion.service.ILabExamBankQuestionService;
import org.jeecg.modules.lab.exam.entity.LabExamBankQuestion;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 试题题库关联表
 * @Author: jeecg-boot
 * @Date:   2023-08-24
 * @Version: V1.0
 */
@Api(tags="试题题库关联表")
@RestController
@RequestMapping("/bankQuestion/labExamBankQuestion")
@Slf4j
public class LabExamBankQuestionController extends JeecgController<LabExamBankQuestion, ILabExamBankQuestionService> {
	@Autowired
	private ILabExamBankQuestionService labExamBankQuestionService;

	@Autowired
	private ILabCronJobService labCronJobService;
	
	/**
	 * 分页列表查询
	 *
	 * @param labExamBankQuestion
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "试题题库关联表-分页列表查询")
	@ApiOperation(value="试题题库关联表-分页列表查询", notes="试题题库关联表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<LabExamBankQuestion>> queryPageList(LabExamBankQuestion labExamBankQuestion,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LabExamBankQuestion> queryWrapper = QueryGenerator.initQueryWrapper(labExamBankQuestion, req.getParameterMap());
		Page<LabExamBankQuestion> page = new Page<LabExamBankQuestion>(pageNo, pageSize);
		IPage<LabExamBankQuestion> pageList = labExamBankQuestionService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param labExamBankQuestion
	 * @return
	 */
	@AutoLog(value = "试题题库关联表-添加")
	@ApiOperation(value="试题题库关联表-添加", notes="试题题库关联表-添加")
//	@RequiresPermissions("bankQuestion:lab_exam_bank_question:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody LabExamBankQuestion labExamBankQuestion) {
		labExamBankQuestionService.save(labExamBankQuestion);

		if (MyEnum.sync) {
			long time = new Date().getTime();
			LabCronJob job = new LabCronJob("lab_exam_bank_question", "0", time, 1);
			labCronJobService.save(job);
		}
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param labExamBankQuestion
	 * @return
	 */
	@AutoLog(value = "试题题库关联表-编辑")
	@ApiOperation(value="试题题库关联表-编辑", notes="试题题库关联表-编辑")
//	@RequiresPermissions("bankQuestion:lab_exam_bank_question:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody LabExamBankQuestion labExamBankQuestion) {
		labExamBankQuestion.setSynFlag("1");
		labExamBankQuestionService.updateById(labExamBankQuestion);

		if (MyEnum.sync) {
			long time = new Date().getTime();
			LabCronJob job = new LabCronJob("lab_exam_bank_question", "1", time, 1);
			labCronJobService.save(job);
		}
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "试题题库关联表-通过id删除")
	@ApiOperation(value="试题题库关联表-通过id删除", notes="试题题库关联表-通过id删除")
//	@RequiresPermissions("bankQuestion:lab_exam_bank_question:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		labExamBankQuestionService.removeById(id);

		if (MyEnum.sync) {
			long time = new Date().getTime();
			String condition="id='"+id+"'";
			LabCronJob job = new LabCronJob("lab_exam_bank_question", "2",condition, time, 1);
			labCronJobService.save(job);
		}
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "试题题库关联表-批量删除")
	@ApiOperation(value="试题题库关联表-批量删除", notes="试题题库关联表-批量删除")
//	@RequiresPermissions("bankQuestion:lab_exam_bank_question:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.labExamBankQuestionService.removeByIds(Arrays.asList(ids.split(",")));

		if (MyEnum.sync) {
			long time = new Date().getTime();
			String[] split = ids.split(",");
			labCronJobService.saveCronBatch("lab_exam_bank_question","2",time,split);
		}
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "试题题库关联表-通过id查询")
	@ApiOperation(value="试题题库关联表-通过id查询", notes="试题题库关联表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<LabExamBankQuestion> queryById(@RequestParam(name="id",required=true) String id) {
		LabExamBankQuestion labExamBankQuestion = labExamBankQuestionService.getById(id);
		if(labExamBankQuestion==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(labExamBankQuestion);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param labExamBankQuestion
    */
//    @RequiresPermissions("bankQuestion:lab_exam_bank_question:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LabExamBankQuestion labExamBankQuestion) {
        return super.exportXls(request, labExamBankQuestion, LabExamBankQuestion.class, "试题题库关联表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
//    @RequiresPermissions("bankQuestion:lab_exam_bank_question:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LabExamBankQuestion.class);
    }

}
